home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib25 / fflush.c < prev    next >
C/C++ Source or Header  |  1992-09-17  |  2KB  |  79 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include "lib.h"
  6. #ifndef _COMPILER_H
  7. #include <compiler.h>
  8. #endif
  9.  
  10. static int _fflush __PROTO((FILE *fp));
  11.  
  12. int fflush(fp)
  13.     register FILE *fp;
  14. /*
  15.  *    implementation note:  This function has the side effect of
  16.  *    re-aligning the virtual file pointer (in the buffer) with
  17.  *    the actual file pointer (in the file) and is therefore used
  18.  *    in other functions to accomplish this re-sync operation.
  19.  */
  20.     {
  21.     register int f, i;
  22.  
  23.      if(fp)
  24.         return(_fflush(fp));
  25.     else
  26.         {
  27.         for(i=0; i<_NFILE; ++i)
  28.             {
  29.             f = _iob[i]._flag;
  30.             if(f & (_IOREAD | _IOWRT | _IORW))
  31.                 _fflush(&_iob[i]);
  32.             }
  33.         return(0);
  34.         }
  35.     }
  36.  
  37. static int _fflush(fp)
  38.     register FILE *fp;
  39.     {
  40.     register int f, rv = 0;
  41.     register long offset;
  42.  
  43.      if(fp == NULL)
  44.         return(0);
  45.     f = fp->_flag;
  46.       if (!(f & (_IORW | _IOREAD | _IOWRT)))  /* file not open */
  47.         return(0);
  48.     if(fp->_cnt > 0)             /* data in the buffer */
  49.         {
  50.           if(f & _IOWRT)                /* writing */
  51.               {
  52.             register long    todo;
  53.  
  54.             /* _cnt is cleared before writing to avoid */
  55.             /* loop if fflush is recursively called by */
  56.             /* exit if ^C is pressed during this write */
  57.             todo = fp->_cnt;
  58.             fp->_cnt = 0;
  59.             if(_write(fp->_file, fp->_base, todo) != todo) 
  60.                 {
  61.                 fp->_flag |= _IOERR;
  62.                 rv = EOF;
  63.                 }
  64.             }
  65.         else if(f & _IOREAD)             /* reading */
  66.             {
  67.             offset = -(fp->_cnt);
  68.             if(lseek(fp->_file, offset, 1) < 0)
  69.                 if(!(f & _IODEV))
  70.                     rv = EOF;
  71.             }
  72.         }
  73.     if(f & _IORW)
  74.         fp->_flag &= ~(_IOREAD | _IOWRT);
  75.     fp->_ptr = fp->_base;
  76.     fp->_cnt = 0;
  77.     return(rv);
  78.     }
  79.